/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Controller.Marque;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class MarqueDAO extends AbstractDAO<Marque> {

  public MarqueDAO(Connection connect) {
	super(connect);
  }

  @Override
  protected boolean insert(Marque obj) {
	throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }

  @Override
  protected boolean update(Marque obj) {
	throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }

  @Override
  protected boolean delete(Marque obj) {
	throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  }

  /**
   * Récupère une marque en fonction de l'id
   * @param id
   * @return Marque ou null
   */
  @Override
  protected Marque findById(int id) {
	Marque marque = new Marque();
	try {

	  ResultSet res = this.connect.createStatement(
			  ResultSet.TYPE_SCROLL_INSENSITIVE,
			  ResultSet.CONCUR_READ_ONLY).executeQuery("SELECT * FROM marque WHERE idM = " + id);

	  if (res.first()) {
		marque.setIdM(res.getInt(1));
		marque.setNomM(res.getString(2));
	  }

	  return marque;

	} catch (Exception e) {
	  e.printStackTrace();
	  return null;
	}
  }

  /**
   * Recupére toutes les marques
   * @return Map<Integer, Marque>
   */
  public Map<Integer, Marque> findAll() {
	Map<Integer, Marque> marques = new HashMap();
	try {
	  ResultSet result = this.connect.createStatement(
			  ResultSet.TYPE_SCROLL_INSENSITIVE,
			  ResultSet.CONCUR_READ_ONLY).executeQuery("SELECT * FROM marque");

	  while (result.next()) {
		Marque marque = new Marque();
		marque.setIdM(result.getInt(1));
		marque.setNomM(result.getString(2));
		marques.put(marque.getIdM(), marque);
	  }
	} catch (Exception e) {
	}
	return marques;
  }
}
